/*
 * Copyright (c) 2017. All  rights reserved.
 * 项目名：microservice-user
 * 文件名：UserUtils.java
 * Date  ：17-11-30 下午3:17
 * Author：abin
 *
 */

package com.stadium.common.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class UserUtils {
    public static String getUserId() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            String user = String.valueOf(authentication.getPrincipal());
            String[] userStr = user.split(",");
            return userStr[0];
        } else {
            return "11";
        }

    }

    public static String getUseName() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            String user = String.valueOf(authentication.getPrincipal());
            String[] userStr = user.split(",");
            return userStr[1];
        } else {
            return "未找到用户";
        }
    }

    public static String getIdNo() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        if (authentication!=null){
            String user = String.valueOf(authentication.getPrincipal());
            String[] userStr = user.split(",");
            return userStr[2];
        }else {
            return "NULL";
        }
    }

    public static String getPhoneNum() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        if (authentication!=null){
            String user = String.valueOf(authentication.getPrincipal());
            String[] userStr = user.split(",");
            return userStr[3];
        }else {
            return "NULL";
        }
    }
}
